package com.dmap.analysispoint.service.impl;

import com.dmap.analysispoint.bo.NameAndValueBo;
import com.dmap.analysispoint.constants.BurialPointConstant;
import com.dmap.analysispoint.controller.background.param.CountParam;
import com.dmap.analysispoint.service.ParamDoService;
import com.dmap.analysispoint.service.PhoneInfoAnalysisService;
import com.dmap.analysispoint.service.TopAnalysisService;
import com.dmap.analysispoint.utils.VersionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * \* @author:zhouhao
 * \* Date:21/05/2018
 * \* Description:
 * \*
 */
@Service
public class TopAnalysisServiceImpl implements TopAnalysisService {

    @Resource
    private PhoneInfoAnalysisService phoneInfoAnalysisService;
    @Resource
    private ParamDoService paramDoService;

    @Override
    public List<List> appVersionTopAnalysisList(CountParam param) {
        CountParam countParam = paramDoService.getParam(param);
        countParam.setEndTime(countParam.getEndTime() + BurialPointConstant.ONE_DAY_TIME_MILLISECONDS - 1);

        //获取统计App各个版本的新增手机数
        List<NameAndValueBo> addPhoneNumList = phoneInfoAnalysisService.countAppVersionAddPhoneNum(countParam);
        List<String> addPhoneVersionNameList = this.getVersionName(addPhoneNumList);
        List<List> countAppVersionAddPhoneNumList = new ArrayList<>();
        countAppVersionAddPhoneNumList.add(0, addPhoneNumList);
        countAppVersionAddPhoneNumList.add(1, addPhoneVersionNameList);

        //获取统计App各个版本的活跃手机数
        List<NameAndValueBo> activePhoneNumList = phoneInfoAnalysisService.countAppVersionActivePhoneNum(countParam);
        List<String> activePhoneVersionNameList = this.getVersionName(activePhoneNumList);
        List<List> countAppVersionActivePhoneNumList = new ArrayList<>();
        countAppVersionActivePhoneNumList.add(0, activePhoneNumList);
        countAppVersionActivePhoneNumList.add(1, activePhoneVersionNameList);

        //获取统计App各个版本的累积手机数
        countParam.setStartTime((long) 0);
        List<NameAndValueBo> allPhoneNumList = phoneInfoAnalysisService.countAppVersionAddPhoneNum(countParam);
        List<String> allPhoneVersionNameList = this.getVersionName(allPhoneNumList);
        List<List> countAppVersionAllPhoneNumList = new ArrayList<>();
        countAppVersionAllPhoneNumList.add(0, allPhoneNumList);
        countAppVersionAllPhoneNumList.add(1, allPhoneVersionNameList);

        //将获取的App各个版本的新增手机数、活跃手机数、累积手机数拼装到AppVersionPhoneNumList中
        List<List> appVersionTopAnalysisList = new ArrayList<>();
        appVersionTopAnalysisList.add(0,countAppVersionAddPhoneNumList);
        appVersionTopAnalysisList.add(1,countAppVersionActivePhoneNumList);
        appVersionTopAnalysisList.add(2,countAppVersionAllPhoneNumList);
        return appVersionTopAnalysisList;
    }

    @Override
    public List<List> firmwareVersionTopAnalysisList(CountParam param) {
        CountParam countParam = paramDoService.getParam(param);
        countParam.setEndTime(countParam.getEndTime() + BurialPointConstant.ONE_DAY_TIME_MILLISECONDS - 1);

        //获取统计设备固件各个版本的新增手机数
        List<NameAndValueBo> addPhoneNumList = phoneInfoAnalysisService.countFirmwareVersionAddPhoneNum(countParam);
        List<String> addPhoneVersionNameList = this.getVersionName(addPhoneNumList);
        List<List> countFirmwareVersionAddPhoneNumList = new ArrayList<>();
        countFirmwareVersionAddPhoneNumList.add(0, addPhoneNumList);
        countFirmwareVersionAddPhoneNumList.add(1, addPhoneVersionNameList);

        //获取统计设备固件各个版本的活跃手机数
        List<NameAndValueBo> activePhoneNumList = phoneInfoAnalysisService.countFirmwareVersionActivePhoneNum(countParam);
        List<String> activePhoneVersionNameList = this.getVersionName(activePhoneNumList);
        List<List> countFirmwareVersionActivePhoneNumList = new ArrayList<>();
        countFirmwareVersionActivePhoneNumList.add(0, activePhoneNumList);
        countFirmwareVersionActivePhoneNumList.add(1, activePhoneVersionNameList);

        //获取统计设备固件各个版本的累积手机数
        countParam.setStartTime((long) 0);
        List<NameAndValueBo> allPhoneNumList = phoneInfoAnalysisService.countFirmwareVersionAddPhoneNum(countParam);
        List<String> allPhoneVersionNameList = this.getVersionName(allPhoneNumList);
        List<List> countFirmwareVersionAllPhoneNumList = new ArrayList<>();
        countFirmwareVersionAllPhoneNumList.add(0, allPhoneNumList);
        countFirmwareVersionAllPhoneNumList.add(1, allPhoneVersionNameList);

        //将获取的设备固件各个版本的新增手机数、活跃手机数、累积手机数拼装到FirmwareVersionPhoneNumList中
        List<List> firmwareVersionTopAnalysisList = new ArrayList<>();
        firmwareVersionTopAnalysisList.add(0,countFirmwareVersionAddPhoneNumList);
        firmwareVersionTopAnalysisList.add(1,countFirmwareVersionActivePhoneNumList);
        firmwareVersionTopAnalysisList.add(2,countFirmwareVersionAllPhoneNumList);
        return firmwareVersionTopAnalysisList;
    }

    @Override
    public List<List> phoneVersionTopAnalysisList(CountParam param) {
        CountParam countParam = paramDoService.getParam(param);
        countParam.setEndTime(countParam.getEndTime() + BurialPointConstant.ONE_DAY_TIME_MILLISECONDS - 1);

        //获取统计手机各个版本的新增手机数
        List<NameAndValueBo> addPhoneNumList = VersionUtils.getPhoneVersion(phoneInfoAnalysisService.countPhoneVersionAddPhoneNum(param));
        List<String> addPhoneVersionNameList = this.getVersionName(addPhoneNumList);
        List<List> countPhoneVersionAddPhoneNumList = new ArrayList<>();
        countPhoneVersionAddPhoneNumList.add(0, addPhoneNumList);
        countPhoneVersionAddPhoneNumList.add(1, addPhoneVersionNameList);

        //获取统计手机各个版本的活跃手机数
        List<NameAndValueBo> activePhoneNumList = VersionUtils.getPhoneVersion(phoneInfoAnalysisService.countPhoneVersionActivePhoneNum(param));
        List<String> activePhoneVersionNameList = this.getVersionName(activePhoneNumList);
        List<List> countPhoneVersionActivePhoneNumList = new ArrayList<>();
        countPhoneVersionActivePhoneNumList.add(0, activePhoneNumList);
        countPhoneVersionActivePhoneNumList.add(1, activePhoneVersionNameList);

        //获取统计手机各个版本的累积手机数
        countParam.setStartTime((long) 0);
        List<NameAndValueBo> allPhoneNumList = VersionUtils.getPhoneVersion(phoneInfoAnalysisService.countPhoneVersionAddPhoneNum(param));
        List<String> allPhoneVersionNameList = this.getVersionName(allPhoneNumList);
        List<List> countPhoneVersionAllPhoneNumList = new ArrayList<>();
        countPhoneVersionAllPhoneNumList.add(0, allPhoneNumList);
        countPhoneVersionAllPhoneNumList.add(1, allPhoneVersionNameList);

        //将获取的设备固件各个版本的新增手机数、活跃手机数、累积手机数拼装到FirmwareVersionPhoneNumList中
        List<List> phoneVersionTopAnalysisList = new ArrayList<>();
        phoneVersionTopAnalysisList.add(0,countPhoneVersionAddPhoneNumList);
        phoneVersionTopAnalysisList.add(1,countPhoneVersionActivePhoneNumList);
        phoneVersionTopAnalysisList.add(2,countPhoneVersionAllPhoneNumList);
        return phoneVersionTopAnalysisList;
    }

    @Override
    public List<List> placeTopAnalysisList(CountParam param) {
        CountParam countParam = paramDoService.getParam(param);
        countParam.setEndTime(countParam.getEndTime() + BurialPointConstant.ONE_DAY_TIME_MILLISECONDS - 1);

        //获取统计各个渠道的新增手机数
        List<NameAndValueBo> addPhoneNumList = phoneInfoAnalysisService.countPlaceAddPhoneNum(param);
        List<String> addPhoneVersionNameList = this.getVersionName(addPhoneNumList);
        List<List> countPlaceAddPhoneNumList = new ArrayList<>();
        countPlaceAddPhoneNumList.add(0, addPhoneNumList);
        countPlaceAddPhoneNumList.add(1, addPhoneVersionNameList);

        //获取统计各个渠道的活跃手机数
        List<NameAndValueBo> activePhoneNumList = phoneInfoAnalysisService.countPlaceActivePhoneNum(param);
        List<String> activePhoneVersionNameList = this.getVersionName(activePhoneNumList);
        List<List> countPlaceActivePhoneNumList = new ArrayList<>();
        countPlaceActivePhoneNumList.add(0, activePhoneNumList);
        countPlaceActivePhoneNumList.add(1, activePhoneVersionNameList);

        //获取统计各个渠道的累积手机数
        countParam.setStartTime((long) 0);
        List<NameAndValueBo> allPhoneNumList = phoneInfoAnalysisService.countPlaceAddPhoneNum(param);
        List<String> allPhoneVersionNameList = this.getVersionName(allPhoneNumList);
        List<List> countPlaceAllPhoneNumList = new ArrayList<>();
        countPlaceAllPhoneNumList.add(0, allPhoneNumList);
        countPlaceAllPhoneNumList.add(1, allPhoneVersionNameList);

        //将获取的设备固件各个版本的新增手机数、活跃手机数、累积手机数拼装到FirmwareVersionPhoneNumList中
        List<List> placeTopAnalysisList = new ArrayList<>();
        placeTopAnalysisList.add(0,countPlaceAddPhoneNumList);
        placeTopAnalysisList.add(1,countPlaceActivePhoneNumList);
        placeTopAnalysisList.add(2,countPlaceAllPhoneNumList);
        return placeTopAnalysisList;
    }

    private List<String> getVersionName(List<NameAndValueBo> nameAndValueBoList) {
        List<String> versionNameList = new ArrayList<>();
        for (NameAndValueBo nameAndValueBo : nameAndValueBoList) {
            versionNameList.add(nameAndValueBo.getName());
        }
        return versionNameList;
    }
}
